package com.ccp.dev.qbdms.controller;

import com.alibaba.fastjson.JSON;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.model.SysOrgDeploy;
import com.ccp.dev.qbdms.service.SysOrgDeployService;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysOrg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统部署方式管理, 信息操作处理控制器
 * 
 * @author zzy
 * @date 2020-08-03 15:43:36
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "系统部署方式管理,")
@RequestMapping("/qbdms/sysOrgDeploy")
public class SysOrgDeployController extends SysBaseController{
	@Resource
	private SysOrgDeployService sysOrgDeployService;
    @Resource
    public ScriptImpl scriptimpl;
    @Resource
    public ExtendSysOrgService extendSysOrgService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zzy
     * @date: 2020-08-03 15:43:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="跳转到系统部署方式管理,首页", exectype = "页面跳转日志")
    public String viewList(ModelMap modelMap) {
        return "sysOrgDeploy/list";
    }

    /**
    * 功能描述: 附带检索条件的分页查询
    *
    * @param: [request] http请求
    * @return: java.lang.Object
    * @auther: zzy
    * @date: 2020-08-03 15:43:36
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description="附带检索条件的分页查询系统部署方式管理,",detail="查询数据信息<#if success>成功<#else></#if>")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, true);
        queryFilter.addFilter("tenantId",ContextUtil.getCurrentUserTenantId());
        //分页数据
        List<SysOrgDeploy> pageDatas = sysOrgDeployService.queryList(queryFilter);
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return success(pageDatas, queryFilter.getPageBean().getTotalCount());
    }


    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: zzy
     * @date: 2020-08-03 15:43:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="跳转到系统部署方式管理,新增页面", exectype = "页面跳转日志")
    public String viewAdd(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        List<SysOrg> orglist = extendSysOrgService.querySubSysOrgBySupOrgId(scriptimpl.getCurrentOrg().getOrgId());
        orglist.add(scriptimpl.getCurrentOrg());
        List<Dictionary> dicList = orglist.stream().map(org -> {
            Dictionary dictionary = new Dictionary();
            dictionary.setItemName(org.getOrgName());
            dictionary.setItemValue(org.getCode());
            return dictionary;
        }).collect(Collectors.toList());
        modelMap.addAttribute("dicList",  JSON.toJSONString(dicList));

        return "sysOrgDeploy/add";
    }

    /**
    * 功能描述:  数据保存通用方法
    *
    * @param: sysOrgDeploy 保存的对象
    * @return: java.lang.Object
    * @auther: zzy
    * @date: 2020-08-03 15:43:36
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description="保存系统部署方式管理,数据",detail="<#if isAdd>新增<#else>编辑</#if>系统部署方式管理,数据<#if success>成功<#else>失败</#if>")
    public Object save(@RequestBody SysOrgDeploy sysOrgDeploy) {
        String id = sysOrgDeploy.getId();
        int result;
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("isAdd",true);
            // 校验选择的组织是否已经配置过
            SysOrgDeploy temp = sysOrgDeployService.getDeployMethodByCode(sysOrgDeploy.getOrgCode());
            if (BeanUtils.isNotEmpty(temp)) {
                SysAuditThreadLocalHolder.putParamerter("success",false);
                return fail("保存失败,选择的组织已经配置过了!");
            }
            sysOrgDeploy.setId(ProjectUUIDUtils.getUUIDFor32());
            sysOrgDeploy.setTenantId(ContextUtil.getCurrentUserTenantId());
            result = sysOrgDeployService.add(sysOrgDeploy);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            //修改操作
            result = sysOrgDeployService.update(sysOrgDeploy);
        }
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }

    /**
    * 功能描述: 跳转到详情页面
    *
    * @return: java.lang.String
    * @auther: zzy
    * @date: 2020-08-03 15:43:36
    */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/particulars")
    @Action(description="跳转到系统部署方式管理,详情页面", exectype = "页面跳转日志")
    public String viewParticulars(HttpServletRequest request,ModelMap modelMap) {
        String id = RequestUtil.getString(request,"id");
        modelMap.put("id",id);
        List<SysOrg> orglist = extendSysOrgService.querySubSysOrgBySupOrgId(scriptimpl.getCurrentOrg().getOrgId());
        orglist.add(scriptimpl.getCurrentOrg());
        List<Dictionary> dicList = orglist.stream().map(org -> {
            Dictionary dictionary = new Dictionary();
            dictionary.setItemName(org.getOrgName());
            dictionary.setItemValue(org.getCode());
            return dictionary;
        }).collect(Collectors.toList());
        modelMap.addAttribute("dicList",  JSON.toJSONString(dicList));
        return "sysOrgDeploy/particulars";
    }

    /**
    * 功能描述: 根据主键id获取一条数据的通用查询方法
    *
    * @param: id 参数对象必须是"id"
    * @return: java.lang.Object
    * @auther: zzy
    * @date: 2020-08-03 15:43:36
    */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description="根据主键id查询系统部署方式管理,的一条数据",detail="根据主键id查询系统部署方式管理,的数据<#if success>成功<#else>失败</#if>")
    public Object get(@RequestParam("id") String id) {
        SysOrgDeploy sysOrgDeploy = sysOrgDeployService.getOneById(id);
        if (null == sysOrgDeploy) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success",true);
        return getResultDataSuccess("get.success",sysOrgDeploy);
    }

    /**
    * 功能描述: 根据主键ID删除数据的通用删除方法
    *
    * @param: id 参数名称必须是"id"
    * @return: java.lang.Object
    * @auther: zzy
    * @date: 2020-08-03 15:43:36
    */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="根据主键ID删除系统部署方式管理,数据",exectype = "删除日志",detail="删除数据信息<#if success>成功<#else>失败</#if>")
    public Object delete(@RequestParam("id") String id) {
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success",false);
            return fail("delete.failure.message");
        }
        int result = sysOrgDeployService.delByIdArray(id.split(","));
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        return result > 0 ? getResultDataSuccess("delete.success") : getResultDataError("delete.failure");
    }


}
